package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.project.entity.BaseResult;
import com.project.entity.Customer;
import com.project.entity.Orders;
import com.project.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
   private OrdersService ordersService;
    @PostMapping("/show")//查看订单
    public BaseResult<Orders> show(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        if(customer==null){return BaseResult.failToken("用户未登录");}
        LambdaQueryWrapper<Orders> lambda = new LambdaQueryWrapper<>();
        lambda.eq(Orders::getOrdersId, customer.getId());
        List<Orders> orders=ordersService.list(lambda);
        return BaseResult.ok("订单:",orders);
    }
    //对当前用户未入住的订单修改
    @PostMapping("/edit")
    public BaseResult<String> edit(@RequestBody Orders orders){
        LambdaUpdateWrapper<Orders> lambda = new LambdaUpdateWrapper<Orders>();
        lambda.set(Orders::getInTime, orders.inTime)
                .set(Orders::getOutTime, orders.outTime)
                .set(Orders::getOrdersNum, orders.ordersNum)
                .set(Orders::getRoomId, orders.roomId)
                .eq(Orders::getOrdersId, orders.getOrdersId())
                .eq(Orders::getStatus, 0);//用户未入住
        if(ordersService.update(lambda)==false)
            return BaseResult.ok("不存在可修改的订单");//提交
        return BaseResult.ok("更新成功");
    }//修改订单
    @PostMapping("delete")
    public BaseResult<String> delete(HttpServletRequest request){
        Customer customer= (Customer) request.getSession().getAttribute("customer");
        LambdaUpdateWrapper<Orders> lambda = new LambdaUpdateWrapper<Orders>();
        lambda.eq(Orders::getOrdersId, customer.getId())
                .eq(Orders::getStatus, 0);;
        if(ordersService.remove(lambda)==false) return BaseResult.fail("订单删除失败");
        //ordersService.removeById();
        return BaseResult.ok("订单已取消");
    }//取消订单
}
